<?php

namespace App\Models\Saas;

use App\Http\Config\ErrorCode;
use App\Library\Tools;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Auth
 * @package app\saas\model
 * 系统表
 */
class Mall extends Model
{
    protected $table = "mall"; //表名
    const CREATED_AT = null;
    const UPDATED_AT = null;

    public static function transferField($data){
        if(!$data){
            return [];
        }
        $data = Tools::transferFields($data);
        return $data;
    }

    //分页获取所有字段数据
    public static function getList($field = '*', $filter = [], $page = 1, $limit = 10, $order = "create_time", $sort = "desc"){
        $db = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field));
        $data['count'] = $db->count();
        $data['currentPage'] = $page;
        $data['totalPage'] = ceil(($data['count'])/$limit);
        $data['list'] = $db->skip(($page - 1) * $limit)->take($limit)->orderBy($order, $sort)->get();
        if($data['list']){
            $data['list'] = self::transferField($data['list']->toArray());
        }
        return $data;
    }

    //添加
    public static function toAdd($data, $getId=false){
        if(!$data)
            return true;
        $adminData = UserManager::query()->where(["id" => $data["manager_id"]])->select(["expired_time", "app_count"])->first();
        if(empty($data["expired_time"])) {
            $data["expired_time"] = $adminData["expired_time"];
        }else{
            $data["expired_time"] = !is_numeric($data["expired_time"]) ? strtotime($data["expired_time"]) : $data["expired_time"];
        }
        if(is_numeric($adminData["app_count"]) && $adminData["app_count"] <= self::query()->where(["manager_id" => $data["manager_id"]])->count()){
            return ErrorCode::code("customized", "已超过可创建系统数量");
        }
        if(empty($data["create_time"])) $data["create_time"] = time();
        if ($getId) {
            return  self::query()->insertGetId($data);
        } else {
            return  self::query()->insert($data);
        }
    }

    //更新
    public static function doUpdate($data, $filter = []){
        if(!$data)
            return true;
        if(!empty($data["expired_time"])) {
            $data["expired_time"] = !is_numeric($data["expired_time"]) ? strtotime($data["expired_time"]) : $data["expired_time"];
        }
        if(!isset($filter["id"])){
            $filter["id"] = self::query()->where($filter)->value("id");
        }
        if(empty($data["update_time"])) $data["update_time"] = time();
        if(self::query()->where($filter)->update($data)){
            return true;
        }else{
            return false;
        }
    }

    //获取多字段数据
    public static function getFields($field, $filter = [], $one = true, $toArray = true){ //缓存方法：缓存所有字段，只返回需要的字段
        if($one){
            $data = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field))->first();
            return $data ? ($toArray ? self::transferField($data->toArray()) : Tools::transferFields($data->toArray())) : [];
        }else{
            $data = self::query()->where($filter)->select(is_array($field) ? $field : explode(",", $field))->get();
            return $data ? ($toArray ? self::transferField($data->toArray()) : $data) : [];
        }
    }

    //根据ID删除数据
    public static function delById($id, $super = false, $managerId = 0){
        if($super) {
            $filter = ["id" => $id];
            if(self::query()->where(["manager_id" => $managerId])->count() <= 1){
                return ErrorCode::code("customized", "超管最后一个系统不能删除");
            }
        }else{
            $filter = ["id" => $id, "manager_id" => $managerId];
        }
        $result = self::query()->where($filter)->delete();
        return $result;
    }
}